<?php
// запрет прямого доступа
defined('_VALID_MOS') or die();
//настройки
$showAllDirTags = $params->get('showAllDirTags', 1);
$directoryId = $params->get('directoryId', 0);
$font_size_min = $params->get('font_size_min', 0);
$font_size_step = $params->get('font_size_step', 0);
//каталоги
$q = " SELECT id, name ";
$q .= " FROM #__boss_config ";
if($showAllDirTags == 0) $q .= " WHERE id IN ($directoryId) ";
$q .= " ORDER BY name";
$database->setQuery($q);
$directories = $database->loadObjectList();
//подготавливаем типы объектов из идов каталогов для запроса
$obj_type = array();
foreach ($directories as $dir){
$obj_type[]= "'com_boss_".$dir->id."'";
}
//запрашиваем тэги соответствующие каталогам
$database->setQuery("SELECT obj_type, tag" .
    " FROM #__content_tags" .
    " WHERE obj_type IN (".implode(', ', $obj_type).") ORDER BY obj_type, tag");
$tags = $database->loadObjectList();
$dirTag = array();
//готовим массив тэгов разбитый по каталогам
foreach($tags as $tag){
    $directoryId = str_replace ( 'com_boss_', '', $tag->obj_type );
    $dirTag[$directoryId][]= $tag->tag;
}

//require_once (JPATH_BASE.'/includes/libraries/tags/tags.php');
class modTagsCloud {

    var $tags;
    var $font_size_min = 14;
    var $font_size_step = 5;

    function __construct($tags) {

        //shuffle($tags);
        $this->tags = $tags;
    }

    function get_tag_count($tag_name, $tags) {

        $count = 0;

        foreach ($tags as $tag) {
            if ($tag == $tag_name) {
                $count++;
            }
        }

        return $count;
    }

    // проверить необходимость
    function tagscloud___($tags) {
        $tags_list = array();

        foreach ($tags as $tag) {
            $tags_list[$tag] = self::get_tag_count($tag, $tags);
        }

        return $tags_list;
    }

    function get_min_count($tags_list) {
        $min = $tags_list[$this->tags[0]];

        foreach ($tags_list as $tag_count) {
            if ($tag_count < $min)
                $min = $tag_count;
        }

        return $min;
    }

    function get_cloud() {

        $cloud = Array();

        $tags_list = self::tagscloud___($this->tags);
        $min_count = self::get_min_count($tags_list);

        foreach ($tags_list as $tag => $count) {

            $font_steps = $count - $min_count;
            $font_size = $this->font_size_min + $this->font_size_step * $font_steps;

            $cloud[$tag][] = $font_size;
            // $cloud['tag'][] =
        }
        return $cloud;
    }

    function modGetBossItemid($directory, $catid) {

        global $_SESSION;

        require_once( JPATH_BASE.'/components/com_boss/boss.tools.php' );

        $itemid = getBossItemid(intval($directory), intval($catid));

        return $itemid;
    }

}
$j=0;
$count_dir = count($directories);
foreach ($directories as $dir){
    $j++;
    if(!isset($dirTag[$dir->id]))
        continue;
    $tags = $dirTag[$dir->id];
    $tagsCloud = new modTagsCloud($tags);
    if($font_size_min >0) $tagsCloud->font_size_min = $font_size_min;
    if($font_size_step >0) $tagsCloud->font_size_step = $font_size_step;
    $t = $tagsCloud->get_cloud();
    $itemid = $tagsCloud->modGetBossItemid($dir->id, 0);
    $i=0;
    $count_t = count($t);
    foreach ($t as $key => $val){
            $i++;
            $link = 'index.php?option=com_boss&task=search_tags&directory='.$dir->id.'&tag='.urlencode($key).'&Itemid='.$itemid;
            echo '<a href="'.$link.'" style="font-size:'.$val[0].'px;">'.$key.'</a>';
            if($i != $count_t) echo ', &nbsp;';

    }
    if($j != $count_dir) echo ', &nbsp;';
}
?>